home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint110s / syscall.spp < prev    next >
Text File  |  1994-02-11  |  14KB  |  477 lines

  1. ; Copyright 1992 Eric R. Smith
  2. ; Copyright 1992,1993 Atari Corporation
  3. ; All rights reserved.
  4.  
  5. %include "magic.i"
  6. ;
  7. ; syscall: interface for system calls. The following entry points are
  8. ;    defined:
  9. ; _mint_bios:  entry point for the BIOS calls (trap #13)
  10. ; _mint_xbios: entry point for XBIOS calls (trap #14)
  11. ; _mint_dos:   entry point for GEMDOS calls (trap #1)
  12. ; _sig_return: user signal handlers return to this routine (see signal.c)
  13. ;              it is responsible for restoring the kernel's old context
  14. ;              via the Psigreturn() system call
  15. ; _lineA0:     calls the line A initialize routine
  16. ; _call_aes:   calls the GEM AES
  17. ; _call_dosound: calls the XBIOS Dosound() function
  18. ; _do_usrcall: calls a user supplied function (e.g. for Supexec), with
  19. ;           arguments supplied from global variables usrarg1, usrarg2, etc.
  20. ; _callout:    calls an external function, after first saving all registers,
  21. ;              and restores the registers afterward
  22. ;
  23. ; external variables referenced:
  24. ; _bios_tab, _bios_max:
  25. ;    table of entry points for BIOS routines, max # of routine
  26. ; _xbios_tab, _xbios_max:
  27. ;    ditto for XBIOS
  28. ; _dos_tab, _dos_max:
  29. ;    ditto for GEMDOS
  30. ; _curproc:
  31. ;    pointer to current process table entry, and hence to save area for
  32. ;    context (this is always the first entry in the PROC table).
  33. ; _valid_return:
  34. ;    used to indicate to the kernel that a valid return from user mode
  35. ;    is taking place
  36. ;
  37. ; _bconbuf, _bconbsiz, _bconbdev:
  38. ;    256 byte buffer for Bconout() output. If _bconbsiz is non-zero,
  39. ;    there are that many bytes in _bconbuf waiting to be flushed. The
  40. ;    output is for device _bconbdev.
  41. ;
  42. ; The C function enter_kernel() is called on entry to the kernel, and the
  43. ; function leave_kernel() is called on exit. These functions are responsible
  44. ; for saving and restoring the various trap vectors, so that MiNT can trap
  45. ; out to TOS directly, but programs can only trap to MiNT.
  46. ;
  47. ; we also call certain BIOS functions directly if these are known not to
  48. ; require saving/restoring of context
  49. ;
  50.     TEXT
  51.     
  52.     XDEF    _mint_bios,_mint_xbios
  53.     XDEF    _mint_dos
  54.     XREF    _build_context
  55.     XREF    _restore_context
  56.     XREF    _proc_clock        ; controls process' allocation of CPU time
  57.     XREF    _enter_kernel
  58.     XREF    _leave_kernel
  59.     XREF    _preempt
  60.  
  61.     XREF    _curproc
  62.     XREF    _bios_tab,_bios_max
  63.     XREF    _xbios_tab,_xbios_max,_old_xbios
  64.     XREF    _dos_tab,_dos_max
  65.  
  66.     XREF    _bconbuf,_bconbsiz,_bconbdev
  67.     XREF    _bflush
  68.  
  69.     XREF    _ubconstat,_do_bconin,_ubcostat,_kbshift
  70.     
  71. _mint_dos:
  72.     clr.w    -(sp)            ; no frame format needed
  73. ; NOTE: FOR NOW, WE PRESERVE A0 ACROSS GEMDOS CALLS. THIS WILL CHANGE
  74. ; SOMEDAY, DON'T RELY ON IT!!!
  75.     move.l    _curproc,d0        ; note: preserve all regs but d0
  76.     addq.l    #4,d0            ; for compatibility
  77.  
  78.     move.l    d0,-(sp)        ; push pointer to syscall context save
  79.     jsr    _build_context
  80.     lea    _dos_tab,a5        ; set syscall_tab
  81.     move.w    _dos_max,d5        ; set syscall_max
  82. ;
  83. ; copy parameters onto process stack. a0 and a1 were set by _build_context
  84. ;
  85.  
  86.     move.l    _curproc,a0
  87.     move.l    (a0),sp            ; this puts us in our private stack
  88.     move.l    24(a1),-(sp)        ; a1 was set by build_context
  89.     move.l    20(a1),-(sp)
  90.     move.l    16(a1),-(sp)
  91.     move.l    12(a1),-(sp)
  92.     move.l    8(a1),-(sp)
  93.     move.l    4(a1),-(sp)
  94.     move.l    (a1),-(sp)
  95.     move.w    #1,-(sp)        ; flag for in GEMDOS
  96.     jsr    _enter_kernel        ; set up vectors appropriately
  97.     addq.w    #2,sp
  98.     bra    _syscall
  99.  
  100. _mint_xbios:
  101. ;
  102. ; Kludge for Setscreen: under Falcon TOS, this
  103. ; calls a GEMDOS function (Srealloc) and so
  104. ; we must *not* change any of the vectors!!
  105.  
  106.     btst    #5,(sp)            ; test for user/super mode
  107.     beq.s    LX_usr
  108. %ifdef ONLY030
  109.     lea    8(sp),a1
  110. %else
  111.     lea    6(sp),a1        ; supervisor mode: args on stack
  112.     tst.w    ($59e).w        ; test longframe
  113.     beq.s    LX_check
  114.     addq.w    #2,a1            ; stack is a bit bigger
  115. %endif
  116.     bra.s    LX_check
  117. LX_usr:
  118.     move.l    usp,a1            ; user mode: args on user stack
  119. LX_check:
  120.     cmp.w    #5,(a1)            ; check for Setscreen command
  121.     beq.s    LX_screen        ; no -- fall through
  122.  
  123.     clr.w    -(sp)            ; no frame format needed
  124.     move.l    _curproc,a0
  125.     pea    4(a0)            ; push pointer to syscall context save
  126.     jsr    _build_context
  127.     lea    _xbios_tab,a5        ; set syscall_tab
  128.     move.w    _xbios_max,d5        ; set syscall_max
  129. ;
  130. ; copy parameters onto process stack. a0 and a1 were set by _build_context
  131. ;
  132.  
  133.     move.l    _curproc,a0
  134.     move.l    (a0),sp            ; this puts us in our private stack
  135.     move.l    24(a1),-(sp)        ; a1 was set by build_context
  136.     move.l    20(a1),-(sp)
  137.     move.l    16(a1),-(sp)
  138.     move.l    12(a1),-(sp)
  139.     move.l    8(a1),-(sp)
  140.     move.l    4(a1),-(sp)
  141.     move.l    (a1),-(sp)
  142.     move.w    #0,-(sp)        ; flag: NOT GEMDOS
  143.     jsr    _enter_kernel        ; set up vectors appropriately
  144.     addq.w    #2,sp
  145.     bra    _syscall
  146. ;
  147. ; For setscreen, jump directly to the ROM vector --
  148. ; this avoids all hazards (like changing the vectors)
  149.  
  150. LX_screen:
  151.     move.l    _old_xbios+8,a0
  152.     jmp    (a0)
  153.  
  154.  
  155. _mint_bios:
  156. ;
  157. ; Entering the kernel can be very expensive; so, we take a short-cut
  158. ; if possible -- we try some BIOS functions out, and if they
  159. ; succeed without blocking then we're done; otherwise, we go
  160. ; through the long procedure for entering the kernel
  161. ;
  162. ; These shortcuts aren't done when we're called in supervisor mode,
  163. ; because TOS uses very tiny stacks (smaller than we want); the
  164. ; shortcuts operate on the user-supplied ssp, whereas the "full"
  165. ; BIOS code works on our (private) system stack
  166. ;
  167. ; the shortcuts are also turned off if BIOSBUF=n
  168. ;
  169.     move.w    #0,-(sp)        ; flag: NOT a GEMDOS call
  170.     jsr    _enter_kernel        ; set up BIOS vectors
  171.     addq.w    #2,sp
  172.     tst.w    _bconbdev        ; is BIOS buffering on?
  173.     bmi    L_bios            ; no; skip all this
  174.  
  175.     btst    #5,(sp)            ; test for user/super mode
  176.     bne.s    L_bios            ; if super, goto L_bios
  177.     tst.w    _proc_clock        ; are we about to be preempted?
  178.     beq.s    L_bios
  179.  
  180.     move.l    usp,a1            ; user mode: args on user stack
  181. L_ubios:
  182.     move.w    (a1),d0            ; get command
  183.     cmp.w    #3,d0            ; Bconout?
  184.     beq    do_bconout        ; yes -- go do it
  185. ;
  186. ; most of the remaining functions require BIOS vectors to be properly
  187. ; set up
  188.     tst.w    _bconbsiz        ; is BIOS output waiting?
  189.     bne.s    L_bios            ; yes -- do regular code
  190.  
  191. ; test for various BIOS functions
  192.     cmp.w    #1,d0            ; Bconstat?
  193.     bne.s    L_00
  194.     move.w    2(a1),-(sp)        ; push arg
  195.     jsr    _ubconstat
  196. L_1out:
  197.     addq.l    #2,sp
  198. L_0out:
  199.     move.l    d0,-(sp)        ; save d0
  200.     ori.w    #$0700,sr        ; spl7()
  201.     jsr    _leave_kernel
  202.     move.l    (sp)+,d0        ; retrieve value to be returned
  203.     rte                ; return to user
  204. L_00:
  205.     cmp.w    #2,d0            ; Bconin?
  206.     bne.s    L_01
  207.     move.w    2(a1),-(sp)        ; yes; push argument
  208.     jsr    _do_bconin
  209.     addq.w    #2,sp
  210.     cmp.w    #$dead,d0        ; would Bconin block?
  211.     bne.s    L_0out            ; no -- we're done
  212.     bra.s    L_bios            ; yes -- do the long stuff
  213. L_01:
  214.     cmp.w    #8,d0            ; Bcostat?
  215.     bne.s    L_02
  216.     move.w    2(a1),-(sp)        ; push device
  217.     jsr    _ubcostat        ; get status
  218.     bra.s    L_1out
  219. L_02:
  220.     cmp.w    #11,d0            ; Kbshift?
  221.     bne.s    L_bios
  222.     move.w    2(a1),-(sp)        ; push arg
  223.     jsr    _kbshift
  224.     bra.s    L_1out
  225.  
  226. L_bios:
  227.     clr.w    -(sp)            ; no frame format needed
  228.     move.l    _curproc,a0
  229.     pea    4(a0)            ; push pointer to syscall context save
  230.     jsr    _build_context
  231.     lea    _bios_tab,a5        ; set syscall_tab
  232.     move.w    _bios_max,d5        ; set syscall_max
  233. ;
  234. ; copy parameters onto process stack. a0 and a1 were set by _build_context
  235. ;
  236.  
  237.     move.l    _curproc,a0
  238.     move.l    (a0),sp            ; this puts us in our private stack
  239.     move.l    24(a1),-(sp)        ; a1 was set by build_context
  240.     move.l    20(a1),-(sp)
  241.     move.l    16(a1),-(sp)
  242.     move.l    12(a1),-(sp)
  243.     move.l    8(a1),-(sp)
  244.     move.l    4(a1),-(sp)
  245.     move.l    (a1),-(sp)
  246.  
  247. _syscall:
  248. ;
  249. ; check here to see if we need to flush the Bconout() buffer
  250. ;
  251.     tst.w    _bconbsiz        ; characters in buffer?
  252.     beq.s    L_noflush        ; no: OK to proceed
  253. ;
  254. ; watch out, this could cause a context switch
  255. ;
  256.     jsr    _bflush            ; flush the buffer
  257.  
  258. L_noflush:
  259. ;
  260. ; figure out which routine to call
  261. ;
  262.     move.w    (sp),d0
  263.     cmp.w    #-1,d0            ; trapping with -1 means return
  264.     bne.s    check_max        ; the corresponding system table
  265.     move.l    a5,d0
  266.     bra.s    out
  267. check_max:
  268.     cmp.w    d5,d0
  269.     bcc.s    error
  270.     add.w    d0,d0
  271.     add.w    d0,d0            ; multiply by 4
  272.     move.l    0(a5,d0.w),d0        ; d0 = syscall_tab[d0]
  273.     beq.s    error            ; null entry means invalid call
  274.     addq.w    #2,sp            ; pop function number off stack
  275.     move.l    d0,a0
  276.     jsr    (a0)            ; go do the call
  277. out:
  278.     move.l    _curproc,a0
  279.     move.l    d0,P_SYSCTXT+C_D0(a0)    ; set d0 in the saved context
  280.     move.w    P_SYSCTXT+C_SR(a0),d0    ; get saved status register
  281.     
  282.     tst.l    P_PTRACER(a0)        ; check curproc->ptracer, if not set
  283.     beq.s    notrace            ; then no pending trace; this ensures
  284.     move.w    d0,d1            ; we work with non-MiNT debuggers
  285.     and.w    #$c000,d1        ; are either of the trace bits set
  286.     sne    P_SYSCTXT+C_PTRACE(a0)    ; mark as trace pending/not
  287. notrace:
  288.     tst.w    _proc_clock        ; has process exceeded time slice?
  289.     bne.s    nosleep            ; no -- continue
  290.     btst    #13,d0            ; caller in supervisor mode?
  291.     bne.s    nosleep            ; yes -- don't interrupt
  292.     tst.w    ($43e).w        ; test floppy disk lock variable
  293.     bne.s    nosleep            ; if locked, can't switch
  294. sleep:
  295.     jsr    _preempt        ; does a sleep(READY_Q)
  296. nosleep:
  297.     ori.w    #$0700,sr        ; spl7()
  298.     jsr    _leave_kernel        ; restore vectors
  299.     move.l    _curproc,a0
  300.     pea    4(a0)
  301.     jsr    _restore_context    ; never returns
  302.  
  303. ;
  304. ; we handle errors by calling through to GEMDOS or the BIOS/XBIOS,
  305. ; as appropriate, and letting them handle it -- that way, if the underlying
  306. ; system has functions we don't know about, they still work
  307. ; to figure out which trap we have to call, we use the system call
  308. ; table placed in a5 earlier
  309.  
  310. error:
  311.     cmp.l    #_xbios_tab,a5
  312.     bne.s    maybe_bios
  313.     trap    #14
  314.     bra.s    out
  315. maybe_bios:
  316.     cmp.l    #_dos_tab,a5
  317.     beq.s    trap_1
  318.     trap    #13
  319.     bra.s    out
  320. trap_1:
  321.     trap    #1
  322.     bra.s    out
  323.  
  324. ;
  325. ; sig_return: user signal handlers return to us. At that point, the
  326. ; stack looks like this:
  327. ;  -4(sp)      (long) sfmt
  328. ;    (sp)      (long) signal number -- was a parameter for user routine
  329. ;
  330.     XDEF    _sig_return
  331.     XREF    _valid_return
  332. _sig_return:
  333.     addq.w    #8,sp            ; pop signal number and sfmt
  334.     move.w    #$11a,-(sp)        ; Psigreturn() system call
  335.     move.w    #1,_valid_return    ; tell kernel it's us!
  336.     trap    #1
  337. ; we had better not come back; if we did, something terrible
  338. ; happened, and we might as well terminate
  339.     move.w    #-998,-(sp)
  340.     move.w    #$4c,-(sp)        ; Pterm()
  341.     trap    #1
  342. ;
  343. ; bconout special code: on entry, a1 points to the stack the user
  344. ; was using. If possible, we just buffer the output until later.
  345. ;
  346.  
  347. do_bconout:
  348.     tst.w    _bconbdev        ; is BIOS buffering on?
  349.     bmi    L_bios            ; no buffering -- skip this code
  350.     move.w    2(a1),d0        ; what device is this for?
  351.     beq    L_bios            ; don't buffer the printer
  352.     cmp.w    _bconbdev,d0        ; same device as is buffered?
  353.     bne.s    new_dev            ; no -- maybe we can't do this
  354. put_buf:
  355.     move.w    4(a1),d0        ; get the character to output
  356.     move.w    _bconbsiz,d1        ; get index into buffer table
  357.     cmp.w    #255,d1            ; buffer full?
  358.     beq    L_bios            ; yes -- flush it out
  359.     lea    _bconbuf,a0
  360.     add.w    d1,a0
  361.     move.b    d0,(a0)            ; store the character
  362.     addq.w    #1,d1
  363.     move.w    d1,_bconbsiz
  364.     jsr    _leave_kernel        ; restore vectors
  365.     moveq.l    #-1,d0            ; return character output OK
  366.     rte
  367.  
  368. new_dev:
  369.     tst.w    _bconbsiz        ; characters already in buffer?
  370.     bne    L_bios            ; yes: we can't buffer this one
  371.     move.w    d0,_bconbdev        ; no: OK, we have a new device
  372.     bra.s    put_buf
  373.  
  374. ;
  375. ; _lineA0: MiNT calls this to get the address of the line A variables
  376. ;
  377.     XDEF    _lineA0
  378. _lineA0:
  379.     movem.l    d2/a2,-(sp)    ; save scratch registers
  380.     dc.w    $a000        ; call the line A initialization routine
  381.     movem.l    (sp)+,d2/a2
  382.     rts
  383.  
  384. ;
  385. ; _call_aes: calls the GEM AES, using the control block passed as
  386. ;            a parameter. Used only for doing appl_init(), to see
  387. ;         if the AES is active yet
  388. ;
  389.     XDEF    _call_aes
  390. _call_aes:
  391.     move.l    4(sp),d1    ; fetch pointer to parameter block
  392.     move.w    #$c8,d0        ; magic number for AES
  393.     movem.l    d2/a2,-(sp)    ; save scratch registers
  394.     trap    #2
  395.     movem.l    (sp)+,d2/a2
  396.     rts
  397. ;
  398. ; _call_dosound:
  399. ;
  400.     XDEF    _call_dosound
  401. _call_dosound:
  402.     move.l    4(sp),d0
  403.     move.l    d0,-(sp)
  404.     move.w    #32,-(sp)
  405.     trap    #14
  406.     addq.w    #6,sp
  407.     rts
  408.  
  409. ;
  410. ; _callout: Call an external function, passing <32 bytes of arguments,
  411. ; and return the value from the function. NOTE: we must be careful
  412. ; to save all registers here!
  413. ;
  414.     XDEF    _callout
  415.     XDEF    _callout1
  416.     XDEF    _callout2
  417. ;
  418. ; _callout is the general purpose one
  419. ;
  420. _callout:
  421.     lea    8(sp),a0        ; pointer to args
  422.     move.l    4(sp),a1        ; pointer to pointer to function
  423.     movem.l    d2-d7/a2-a6,-(sp)    ; save registers
  424.     movem.l    (a0),d0-d7        ; copy parameters
  425.     movem.l    d0-d7,-(sp)
  426.     suba.l    a5,a5            ; the BIOS expects 0 in a5
  427.     jsr    (a1)            ; go do it
  428.     lea    32(sp),sp
  429.     movem.l    (sp)+,d2-d7/a2-a6    ; restore reggies
  430.     rts
  431. ;
  432. ; _callout2 and _callout1 are for functions with just 1 or
  433. ; 2 16 bit parameters. We cheat and use the same code for
  434. ; both, since passing 32 bits isn't much more expensive than
  435. ; passing 16 bits (and since the called function will just
  436. ; ignore any extra arg)
  437. ;
  438.  
  439. _callout1:
  440. _callout2:
  441.     movem.l    4(sp),a0/a1        ; get function ptr & args
  442.     movem.l    d2-d7/a2-a6,-(sp)    ; save reggies
  443.     move.l    a1,-(sp)        ; push args
  444.     suba.l    a5,a5            ; the BIOS expects 0 in a5
  445.     jsr    (a0)            ; do function
  446.     addq.w    #4,sp
  447.     movem.l    (sp)+,d2-d7/a2-a6    ; restore reggies
  448.     rts
  449.  
  450. ;
  451. ; do_usrcall: call the user supplied function (*usrcall)(), with
  452. ; arguments given in the longwords usrarg1..usrarg5. Return value
  453. ; is placed in usrret. This function is used by the Supexec code
  454. ; in xbios.c.
  455.  
  456.     XDEF    _do_usrcall
  457.     XREF    _usrret
  458.     XREF    _usrcall
  459.     XREF    _usrarg1,_usrarg2,_usrarg3,_usrarg4,_usrarg5
  460.  
  461. _do_usrcall:
  462.     move.l    _usrarg5,-(sp)
  463.     move.l    _usrarg4,-(sp)
  464.     move.l    _usrarg3,-(sp)
  465.     move.l    _usrarg2,-(sp)
  466.     move.l    _usrarg1,-(sp)
  467.     move.l    _usrcall,-(sp)    ; the user expects to see this on the stack
  468.     pea    ucret(pc)    ; so rts puts us back here
  469.     move.l    _usrcall,-(sp)    ; this copy is for us to use
  470.     rts            ; to jump to (we don't want to use registers)
  471. ucret:
  472.     lea    24(sp),sp    ; fix up stack
  473.     move.l    d0,_usrret    ; save returned value
  474.     rts            ; back to caller
  475.  
  476.     END
  477.